﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace MyGames
{
    public enum GameState { TitleScreen, Playing, Pause };

    public class TCOGame : Microsoft.Xna.Framework.Game
    {
        #region Declaration
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        static public GameState GameState;
        #endregion

        #region Constructor
        public TCOGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = Const.ScreenWidth;
            graphics.PreferredBackBufferHeight = Const.ScreenHeight;
            graphics.ApplyChanges();
        }

        protected override void Initialize()
        {
            //ModelsBuilder builder = new ModelsBuilder();
            //builder.CreateXml();
            //MapBuilder mapsbuilder = new MapBuilder();
            //mapsbuilder.CreateAllMapsXml();

            // TODO: Add your initialization logic here
            GameLoader.InitGameData(Content);       // Load content (fonts, graphics, data, audio..)
            InputManager.Initialize();              // Init input manager
            //AudioManager.Initialize(this);          // Init sound manager
            IsMouseVisible = true;

            Camera.InitCamera(new Vector2(Const.ScreenWidth, Const.ScreenHeight));
            TitleScreen.InitTitleScreen();
            GamesManager.InitGameManager();

            GameState = GameState.TitleScreen;
            base.Initialize();
        }
        #endregion

        #region Helper Methods
        #endregion

        protected override void Update(GameTime gameTime)
        {
            // Update input
            InputManager.Update();

            // In title screen
            if (GameState == MyGames.GameState.Pause || GameState == MyGames.GameState.TitleScreen)
            {
                // Main update
                TitleScreen.Update(gameTime);

                // Check for new game
                if (TitleScreen.State == TitleScreen.TitleScreenState.NewGame)
                    GamesManager.StartNewGame(TitleScreen.Difficult);

                // Check for load game
                if (TitleScreen.State == TitleScreen.TitleScreenState.Continue)
                    GamesManager.ContinueGame();

                //// Check for quit game
                if (TitleScreen.State == TitleScreen.TitleScreenState.Quit)
                    this.Exit();
            }

            // In game screen
            if (GameState == MyGames.GameState.Playing)
            {
                GamesManager.Update(gameTime);
            }

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);

            // Draw title screen
            if (GameState == MyGames.GameState.TitleScreen || GameState == MyGames.GameState.Pause)
                TitleScreen.Draw(spriteBatch);

            // Draw in game screen
            if (GameState == MyGames.GameState.Playing)
                GamesManager.Draw(spriteBatch);


            spriteBatch.End();
            base.Draw(gameTime);
        }

        #region Load Content
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
        }
        #endregion

        #region Unload Content
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }
        #endregion
    }
}
